package com.aiyige.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.widget.ImageView;

import com.aiyige.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import org.w3c.dom.Text;

/**
 * Created by zhuyakun on 2018/2/6.
 */

public class GlideUtil {
    public static GlideBuilder with(Context context) {
        return new GlideBuilder(context);
    }

    public static class GlideBuilder {
        public static final int IMAGE_SIZE_ORIGINAL = 0;
        public static final int IMAGE_SIZE_BIG = 1;
        public static final int IMAGE_SIZE_MIDDLE = 2;
        public static final int IMAGE_SIZE_SMALL = 3;
        Context context;
        Integer imageSize = null;
        boolean asGif = false;


        public GlideBuilder(Context context) {
            this.context = context;
        }

        public GlideBuilder asGif() {
            asGif = true;
            return this;
        }

        public GlideBuilder original() {
            imageSize = IMAGE_SIZE_ORIGINAL;
            return this;
        }


        public GlideBuilder big() {
            imageSize = IMAGE_SIZE_BIG;
            return this;
        }

        public GlideBuilder middle() {
            imageSize = IMAGE_SIZE_MIDDLE;
            return this;
        }


        public GlideBuilder small() {
            imageSize = IMAGE_SIZE_SMALL;
            return this;
        }


        public RequestBuilder<?> loadAvatar(String url) {
            if (imageSize == null) {
                imageSize = IMAGE_SIZE_SMALL;
            }
            if (asGif) {
                return Glide.with(context)
                        .load(modifyUrl(url, false))
                        .apply(RequestOptions.circleCropTransform())
                        .apply(RequestOptions.placeholderOf(R.drawable.default_head))
                        .error(Glide.with(context)
                                .load(url)
                                .apply(RequestOptions.circleCropTransform())
                                .apply(RequestOptions.placeholderOf(R.drawable.default_head)));
            } else {
                return Glide.with(context)
                        .asBitmap()
                        .load(modifyUrl(url, true))
                        .apply(RequestOptions.circleCropTransform())
                        .apply(RequestOptions.placeholderOf(R.drawable.default_head))
                        .error(Glide.with(context)
                                .asBitmap()
                                .load(url)
                                .apply(RequestOptions.circleCropTransform())
                                .apply(RequestOptions.placeholderOf(R.drawable.default_head)));
            }
        }

        public RequestBuilder<?> loadCover(String url) {
            if (imageSize == null) {
                imageSize = IMAGE_SIZE_MIDDLE;
            }
            if (asGif) {
                return Glide.with(context)
                        .load(modifyUrl(url, false))
                        .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
//                        .thumbnail(Glide.with(context)
//                                .load(WebImageUrlModifier.jpgThumbnail(url))
//                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg)))
                        .error(Glide.with(context)
                                .load(url)
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg)));
            } else {
                return Glide.with(context)
                        .asBitmap()
                        .load(modifyUrl(url, true))
                        .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg))
//                        .thumbnail(Glide.with(context)
//                                .asBitmap()
//                                .load(WebImageUrlModifier.jpgThumbnail(url))
//                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg)))
                        .error(Glide.with(context)
                                .asBitmap()
                                .load(url)
                                .apply(RequestOptions.placeholderOf(R.drawable.default_image_bg)));

            }
        }

        private String modifyUrl(String url, boolean asJpg) {
            if (TextUtils.isEmpty(url)) {
                return "";
            }
            String resultUrl;
            switch (imageSize) {
                case IMAGE_SIZE_BIG:
                    resultUrl = asJpg ? WebImageUrlModifier.bigJpgImage(url) : WebImageUrlModifier.bigImage(url);
                    break;
                case IMAGE_SIZE_MIDDLE:
                    resultUrl = asJpg ? WebImageUrlModifier.mediumJpgImage(url) : WebImageUrlModifier.mediumImage(url);
                    break;
                case IMAGE_SIZE_SMALL:
                    resultUrl = asJpg ? WebImageUrlModifier.smallJpgImage(url) : WebImageUrlModifier.smallImage(url);
                    break;
                default:
                    resultUrl = url;
                    break;
            }
            return resultUrl;
        }


        /**
         * 播放gif限制次数
         *
         * @param imageView 目标imageView
         * @param gifId     gif的资源id
         * @param times     0表示无限次数一直播放
         */
        public void loadGif(final ImageView imageView, int gifId, final int times) {
            if (imageView == null) {
                return;
            }
            Glide.with(context).load(gifId).into(new SimpleTarget<Drawable>() {
                @Override
                public void onResourceReady(Drawable drawable, Transition<? super Drawable> transition) {
                    if (drawable instanceof GifDrawable) {
                        GifDrawable gifDrawable = (GifDrawable) drawable;
                        gifDrawable.setLoopCount(times);
                        imageView.setImageDrawable(drawable);
                        gifDrawable.start();
                    }
                }
            });
        }

    }
}
